const { Service } = require("egg")
class BookCategoryService extends Service {
  async list(pageSize, pageNum, where) {
    const data = await this.app.mysql.select("egg_category", {
      where,
      orders: [
        ["id", "asc"],
      ],
      offset: (pageNum - 1) * pageSize,
      limit: pageSize,
    })
    const total = await this.app.mysql.count("egg_category", where)
    return { data, total }
  }
  async single(category_id) {
    const result = await this.app.mysql.get("egg_category", { category_id })
    return result
  }
  async isExist(categoryName) {
    //判断数据库是否已经存在当前添加的分类
    const exists = await this.app.mysql.query(
      `SELECT egg_category.category_name FROM egg_category WHERE category_name LIKE ?`,
      [categoryName]
    )
    return exists
  }
  async create(bookcategory) {
    const result = await this.app.mysql.insert("egg_category", bookcategory)
    return result
  }
  async update(bookcategory) {
    const result = await this.app.mysql.update("egg_category", bookcategory)
    return result
  }
  async destroy(id) {
    const result = await this.app.mysql.delete("egg_category", { id })
    return result
  }
  async getCateGorySingleInfoAll(id) {
    const result = await this.app.mysql.query(
      `SELECT egg_book.* from egg_book_category
    INNER JOIN egg_book 
    on egg_book.id = egg_book_category.book_id 
    WHERE egg_book_category.category_id = ?`,
      [id]
    )
    return result
  }
  
}
module.exports = BookCategoryService
